package com.ruoyi.ly.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ly.domain.dto.PageProjectDTO;
import com.ruoyi.ly.domain.dto.PageServeUserDTO;
import com.ruoyi.ly.domain.vo.InspectionTreeVO;
import com.ruoyi.ly.domain.vo.ServeFlowAPPVO;
import com.ruoyi.ly.domain.vo.ServePlanAPPVO;
import com.ruoyi.ly.service.ProjectService;
import com.ruoyi.ly.service.SpaceBanService;
import com.ruoyi.ly.utils.PageResult;
import com.ruoyi.ly.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ly.domain.ServeExaminePlan;
import com.ruoyi.ly.service.IServeExaminePlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 巡检计划Controller
 * 
 * @author wj
 * @date 2024-01-13
 */
@RestController
@RequestMapping("/ctnm-ly/plan")
@Slf4j
public class ServeExaminePlanController extends BaseController
{
    @Autowired
    private IServeExaminePlanService serveExaminePlanService;

    @Autowired
    private SpaceBanService spaceBanService;

    /**
     * 查询巡检计划列表
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(ServeExaminePlan serveExaminePlan)
    {
        startPage();
        List<ServeExaminePlan> list = serveExaminePlanService.selectServeExaminePlanList(serveExaminePlan);
        return getDataTable(list);
    }

    /**
     * 导出巡检计划列表
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:export')")
    @Log(title = "巡检计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ServeExaminePlan serveExaminePlan)
    {
        List<ServeExaminePlan> list = serveExaminePlanService.selectServeExaminePlanList(serveExaminePlan);
        ExcelUtil<ServeExaminePlan> util = new ExcelUtil<ServeExaminePlan>(ServeExaminePlan.class);
        util.exportExcel(response, list, "巡检计划数据");
    }

    /**
     * 获取巡检计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:query')")
    @GetMapping(value = "/{examinePlanId}")
    public AjaxResult getInfo(@PathVariable("examinePlanId") Long examinePlanId)
    {
        return success(serveExaminePlanService.selectServeExaminePlanByExaminePlanId(examinePlanId));
    }

    /**
     * 新增巡检计划
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:add')")
    @Log(title = "巡检计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ServeExaminePlan serveExaminePlan)
    {
        return toAjax(serveExaminePlanService.insertServeExaminePlan(serveExaminePlan));
    }

    /**
     * 修改巡检计划
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:edit')")
    @Log(title = "巡检计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ServeExaminePlan serveExaminePlan)
    {
        return toAjax(serveExaminePlanService.updateServeExaminePlan(serveExaminePlan));
    }

    /**
     * 删除巡检计划
     */
    @PreAuthorize("@ss.hasPermi('ctnm-ly:plan:remove')")
    @Log(title = "巡检计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examinePlanIds}")
    public AjaxResult remove(@PathVariable Long[] examinePlanIds)
    {
        return toAjax(serveExaminePlanService.deleteServeExaminePlanByExaminePlanIds(examinePlanIds));
    }


    /**
     * 查询选择巡检区域
     */

    @ApiOperation("查询选择巡检区域")
    @GetMapping("/selectInspection/{examineCateType}")
    public Result<List<InspectionTreeVO>> selectInspection(@PathVariable("examineCateType") String examineCateType) {
        log.info("查询选择巡检区域：{}",examineCateType);
        List<InspectionTreeVO> inspectionTreeVOList = spaceBanService.selectInspection(examineCateType);
        return Result.success(inspectionTreeVOList);
    }

    /**
     * 管理员分页查询
     */
    @ApiOperation("管理员分页查询")
    @GetMapping("/admin/list")
    public Result<PageResult> page(PageServeUserDTO pageServeUserDTO) {
        log.info("管理员分页查询：{}", pageServeUserDTO);
        PageResult pageResult = serveExaminePlanService.pageQuery(pageServeUserDTO);
        return Result.success(pageResult);
    }

    //根据openId显示对应的巡检信息(小程序端)
    @ApiOperation("根据openId显示对应的巡检计划信息")
    @GetMapping("/selectByOpenId/{openId}/{examineCateType}")
    public  Result<List<ServePlanAPPVO>> selectByOpenId(@PathVariable String openId,@PathVariable String examineCateType){
        log.info("根据openId显示对应的巡检计划信息：{}",openId);
        List<ServePlanAPPVO> servePlanAPPVOList = serveExaminePlanService.selectByOpenId(openId,examineCateType);
        return Result.success(servePlanAPPVOList);
    }

    //根据巡检计划id显示对应的巡检计划信息(小程序端)   keyId 用于区分待巡检和已巡检
    @ApiOperation("根据巡检计划id显示对应的巡检计划信息")
    @GetMapping("/selectByPlanId/{examinePlanId}/{keyId}")
    public  Result<List<ServeFlowAPPVO>> selectByPlanId(@PathVariable Long examinePlanId, @PathVariable Long keyId){
        log.info("根据巡检计划id显示对应的巡检计划信息：{}",examinePlanId);
        List<ServeFlowAPPVO> serveFlowAPPVOList = serveExaminePlanService.selectByPlanId(examinePlanId, keyId);
        return Result.success(serveFlowAPPVOList);
    }

    //修改对应巡检人
    @ApiOperation("修改对应巡检人")
    @PutMapping("/update/{name}/{examinePlanId}")
    public Result update(@PathVariable String name,@PathVariable Long examinePlanId){
        log.info("修改对应巡检人：{}",name);
        serveExaminePlanService.update(name,examinePlanId);
        return Result.success();
    }

}
